// pages/classic/classic.js
import {ClassicModel} from '../../models/classic.js'
import {LikeModel} from '../../models/like.js'

let classicModel = new ClassicModel
let likeModel = new LikeModel

Page({

  /**
   * 页面的初始数据
   */
  data: {
    classic: null,
    latest: true,
    first: false,
    starCount: 0,
    starStatus: false
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    classicModel.getLatest((res) => {
      // 将获取到的数据传递到组件中
      this.setData({
        // ...res
        classic: res,
        starCount: res.fav_nums,
        starStatus: res.like_status
      })
    })
  },

  /* 自定义... */
  onLike:function (event) {
    let behavior = event.detail.behavior
    let classicData = this.data.classic
    likeModel.like(behavior, classicData.id, classicData.type)
  },
  onNext: function (event) {
    this._updateClassic('next')
  },
  onPrevious:function(event){
    this._updateClassic('previous')
  },
  _updateClassic: function(targetURL) {
    let index = this.data.classic.index
    // 调用获取期刊模块，发送POST请求用新数据替换classic，并动态判断当前期是不是第一（最后一）期
    classicModel.getClassic(index, targetURL,(res) => {
      this._getLikeStasus(res.id, res.type)
      this.setData({
        classic: res,
        latest:classicModel.isLatest(res.index),
        first:classicModel.isFirst(res.index)
      })
    })
  },

  _getLikeStasus: function (artID, category) {
    /* body... */
    likeModel.getClassicLikeStatus(artID, category, (res)=>{
      this.setData({
        starCount: res.fav_nums,
        starStatus: res.like_status
      })
    })
  },



  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})